package cn.itsource.aigou.service.impl;

import cn.itsource.aigou.domain.ProductType;
import cn.itsource.aigou.mapper.ProductTypeMapper;
import cn.itsource.aigou.service.IProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品目录 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2019-07-31
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Override
    public List<ProductType> tree() {
        //定义一个list装一级菜单
        List<ProductType> list = new ArrayList<>();
        //定义一个map装其他菜单
        Map<Long , ProductType> map = new HashMap();
        List<ProductType> productTypes = baseMapper.selectList(null);
        for (ProductType productType : productTypes) {
            map.put(productType.getId(),productType);
        }
        for (ProductType productType : productTypes) {
            if (productType.getPid()==0){
                list.add(productType);
            }else {
                ProductType productType1 = map.get(productType.getPid());
                productType1.getChildren().add(productType);
            }
        }
        return list;
    }
}
